<?php

namespace App\Http\Controllers\Otc;

use App\Http\Controllers\Controller;
use App\Library\Yunxin;
use App\Models\Autonym;
use App\Models\Currency;
use App\Models\Property;
use App\Models\EmailSms;
use App\Models\Enter;
use App\Models\PhoneSms;
use App\Models\Unbind;
use App\Models\User;
use App\Models\UserAlipay;
use App\Models\UserWechat;
use Illuminate\Http\Request;
use App\Library\Orders\Orders;
use DB;


class CenterController extends Controller
{
    /**
     * 修改头像
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update_headimg(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid', 'headimg')) {
            //错误提示
            return respF(1003);
        }

        //用户id
        $uid = $request->get('uid');

        //头像路径
        $headimg = $request->get('headimg');
        //修改路径-zrc 2018-5-26
        if(strpos($headimg,'//')){
            $img_s = parse_url($headimg);
            $headimg = ltrim($img_s['path'],'/');
        }
        //end 2018-5-26
        //查找用户
        $user = User::find($uid);
        //用户不存在
        if (!$user) {
            //错误提示
            return respF(1001);
        }
        //更新头像
        $user->update(['headimg' => $headimg]);
        //调用云信更新信息
        $model = new Yunxin\Yunxin();
        $arr   = $model->change_yx($user->id, $user->nickname, config('alioss.host').'/'.$headimg, $user->token);

        //成功提示
        return response()->json(['status' => 1, 'msg' => '修改成功']);
    }

    /**
     * 修改昵称
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update_nickname(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid', 'nickname')) {
            //错误提示
            return respF(1003);
        }
        //用户id
        $uid = $request->get('uid');
        //昵称
        $nickname = $request->get('nickname');
        //查找用户
        $user = User::find($uid);
        //用户不存在
        if (!$user) {
            //错误提示
            return respF(1001);
        }
        //更新昵称
        $user->update(['nickname' => $nickname]);
        //调用云信更新信息
        $model = new Yunxin\Yunxin();
        $model->change_yx($user->id, $nickname, '', '');
        //成功提示
        return response()->json(['status' => 1, 'msg' => '修改成功']);
    }

    /**
     * 绑定支付宝
     * @author hxq
     * @param  Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bind_alipay(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid', 'alipay_account', 'alipay_code', 'pay_pass', 'real_name')) {
            //返回错误提示
            return respF(1003);
        }
        //用户id
        $uid = $request->get('uid');
        //支付宝账号
        $alipay_account = $request->get('alipay_account');
        //二维码图片
        $alipay_code = $request->get('alipay_code');
        //修改路径-zrc 2018-5-26
        if(strpos($alipay_code,'//')){
            $img_s = parse_url($alipay_code);
            $alipay_code = ltrim($img_s['path'],'/');
        }
        //end 2018-5-26
        //交易密码
        $pay_pass = $request->get('pay_pass');
        //查找用户
        $user = User::find($uid);
        //用户不存在
        if (!$user) {
            //返回错误提示
            return respF(1001);
        }
        //验证交易密码
        if ($user->pay_pass != md5($pay_pass)) {
            //返回错误提示
            return respF(2000);
        }
        //真实姓名
        $real_name = $request->get('real_name');
        /*$find = UserAlipay::where('uid', $uid)->first();
        if ($find) {
        return response()->json(['status' => 0, 'msg' => '已绑定']);
        }*/
        //插入数据库
        UserAlipay::updateOrCreate(
            [
                'uid' => $uid,
            ],
            [
                'alipay_account' => $alipay_account,
                'alipay_code'    => $alipay_code,
                'real_name'      => $real_name,
            ]
        );
        //成功提示
        return response()->json(['status' => 1, 'msg' => '保存成功']);
    }

    /**
     * 绑定微信
     * @author hxq
     * @param  Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bind_wechat(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid', 'wechat_account', 'wechat_code', 'pay_pass', 'real_name')) {
            //返回错误提示
            return respF(1003);
        }
        //用户id
        $uid = $request->get('uid');
        //微信账号
        $wechat_account = $request->get('wechat_account');
        //微信二维码图片
        $wechat_code = $request->get('wechat_code');
        //修改路径-zrc 2018-5-26
        if(strpos($wechat_code,'//')){
            $img_s = parse_url($wechat_code);
            $wechat_code = ltrim($img_s['path'],'/');
        }
        //end 2018-5-26
        //交易密码
        $pay_pass = $request->get('pay_pass');
        //查找用户
        $user = User::find($uid);
        //用户不存在
        if (!$user) {
            //返回错误提示
            return respF(1001);
        }
        //验证交易密码
        if ($user->pay_pass != md5($pay_pass)) {
            //返回错误提示
            return respF(2000);
        }
        //真实姓名
        $real_name = $request->get('real_name');
        //插入数据库
        UserWechat::updateOrCreate(
            [
                'uid' => $uid,
            ],
            [
                'wechat_account' => $wechat_account,
                'wechat_code'    => $wechat_code,
                'real_name'      => $real_name,
            ]
        );
        //成功提示
        return response()->json(['status' => 1, 'msg' => '保存成功']);
    }

    /**
     * 商家解绑申请
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function unbind_apply(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid')) {
            //错误提示
            return respF(1003);
        }
        //用户id
        $uid = $request->get('uid');
        //查找用户
        $user = User::find($uid);
        //用户不存在
        if (!$user) {
            //错误提示
            return respF(1001);
        }
        //查询是否已提交
        $unbind = Unbind::where('uid', $uid)->first();
        //没有解绑申请
        if (collect($unbind)->count() == 0) {
            //写入申请表
            Unbind::create([
                'uid' => $uid,
            ]);

            //成功提示
            return response()->json(['status' => 1, 'msg' => '申请成功']);
        } else {
            //请勿重复提交
            return respF(3008);            
        }
    }

    /**
     * 商家入驻申请
     * @author hxq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function enter_apply(Request $request)
    {
        $orders_model = new Orders();
        //验证必传参数
        if (!$request->has('uid', 'phone', 'qq', 'email')) {
            //缺少参数
            return respF(1003);
        }
        $uid   = $request->get('uid');
        $phone = $request->get('phone');
        $qq    = $request->get('qq');
        $email = $request->get('email');
        //验证参数是否为空
        if (empty($uid) || empty($phone) || empty($qq) || empty($email)) {
            return respF(1000);
        }
        //判断是否实名
        $autonym = Autonym::where(['uid' => $uid, 'status' => 2])->first();
        //没有实名
        if (!$autonym) {
            //实名认证未通过
            return respF(2005);
        }
        //判断是否有入住
        $enter = Enter::where('uid', $uid)->first();
        //判断资金是否足够
//        $usdt_id = $orders_model->get_standard();
//        $p_where['cid'] = $usdt_id;
//        $p_where['uid'] = $uid;
//        $money = 3000;
//        $property = Property::where($p_where)->first();
//        if (empty($property)||$property['balance']<$money){
//            return respF(4012);
//        }
        //没有或者失败的情况下才能提交
        if ($enter) {
            if ($enter->status == 1)
                return respF(3008);
            $where1['uid'] = $uid;



            try {
                //开启事务
                DB::beginTransaction();
                Enter::where($where1)->update(
                    [
                        'phone'  => $phone,
                        'qq'     => $qq,
                        'email'  => $email,
                        'status' => 0,
                    ]);
                //Property::where($p_where)->increment('blocked',$money);
                //Property::where($p_where)->decrement('balance',$money);
                DB::commit();
            } catch (Exception $e) {
                DB::rollback();
                //$msg = $e->getMessage();//接收错误号
                return respF(4013);
            }
        }else{
            try {
                //开启事务
                DB::beginTransaction();
                Enter::create(
                    [
                        'uid' => $uid,
                        'phone'  => $phone,
                        'qq'     => $qq,
                        'email'  => $email,
                        'status' => 0,
                    ]);
                //Property::where($p_where)->increment('blocked',$money);
                //Property::where($p_where)->decrement('balance',$money);
                DB::commit();
            } catch (Exception $e) {
                DB::rollback();
                //$msg = $e->getMessage();//接收错误号
                return respF(4013);
            }
        }
        return response()->json(['status' => 1, 'msg' => '提交成功', 'data' => ['status' => 0]]);

    }

    /**
     * 资产认证
     * @author hxq
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function property_ident(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid', 'pay_pass', 'account', 'code')) {
            //缺少参数
            return respF(1003);
        }
        //用户ID
        $uid = $request->get('uid');
        //入驻申请
        $enter = Enter::where('uid', $uid)->first();
        //没有入驻
        if (!$enter) {
            //请先入驻
            return respF(3004);
        }
        //查找用户
        $user = User::find($uid);
        //未找到用户
        if (!$user) {
            //用户不存在
            return respF(1001);
        }
        //判断资产是否有3000OBA
        $cid = Currency::where('name_en', 'OBA')->value('id');
        $p_where['cid'] = $cid;
        $p_where['uid'] = $uid;
        $money = 30000;
        $property = Property::where($p_where)->first();
        if (empty($property)||$property['balance']<$money){
            return respF(1036);
        }

        //交易密码
        $pay_pass = $request->get('pay_pass');
        //判断交易密码
        if (strlen($user->pay_pass) == 0) {
            //未设置交易密码
            return respF(2024);
        }
        //验证交易密码
        if ($user->pay_pass != md5($pay_pass)) {
            //交易密码错误
            return respF(2000);
        }
        //账号
        $account = $request->get('account');
        //判断手机还是邮箱
        if (isPhone($account)) {
            //查找手机验证码
            $sms = PhoneSms::where(['phone' => $account, 'type' => 5])->first();
        } else if (isEmail($account)) {
            $sms = EmailSms::where(['email' => $account, 'type' => 5])->first();
        } else {
            //格式错误
            return respF(2011);
        }
        //验证码
        $code = $request->get('code');
        //验证码验证
        /*if (!$sms) {
        //throw new Exception('请获取验证码');
        return respF(3005);
        }
        if ($sms->expire_time < time()) {
        //throw new Exception('验证码已过期');
        return respF(3006);
        }
        if ($code != $sms->code) {
        //throw new Exception('验证码不正确');
        return respF(3007);
        }
         */

        //修改入驻申请状态
        if ($enter->status == 0) {
            try {
                //开启事务
                DB::beginTransaction();
                $enter->update([
                    'status' => 3
                ]);
                Property::where($p_where)->increment('blocked',$money);
                Property::where($p_where)->decrement('balance',$money);
                DB::commit();
            } catch (Exception $e) {
                DB::rollback();
                //$msg = $e->getMessage();//接收错误号
                return respF(4013);
            }
        }
        //返回参数
        $data['enter']['uid']    = $uid;
        $data['enter']['status'] = 3;
        return response()->json(['status' => 1, 'msg' => '成功', 'data' => $data]);
    }
}
